from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from geektime_0.web.wework.framework.base_page import BasePage
from geektime_0.web.wework.page.contact_page import ContactPage

# 继承BasePage
class WeWorkPage(BasePage):
    def __init__(self, driver = None):
        # 调用父类的init方法，初始化driver
        super().__init__(driver)
        self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')

    # 通讯录
    def contact(self):
        # 显式等待的高级用法，使用复杂行为解决点击不生效问题
        def loop_click(driver):
            # self.driver.find_element(By.LINK_TEXT, '通讯录').click()
            self.click(By.LINK_TEXT, '通讯录')
            return len(self.driver.find_elements(By.LINK_TEXT, '删除')) > 0

        WebDriverWait(self.driver, 10).until(loop_click)
        return ContactPage(self.driver)

    # 进入首页
    def portal(self):
        # self.driver.find_element(By.LINK_TEXT, '首页').click()
        self.click(By.LINK_TEXT, '首页')
        # 返回当前页面
        return self
